<?php

require_once($INCLUDE_PATH.'app/controller/InitController.php');
require_once ($INCLUDE_PATH . "/inc/checkfun.php");
require_once ($INCLUDE_PATH . "/include/checklogin.php");
require_once ($INCLUDE_PATH . "/include/WebService2Db.php");
function __autoload($class_name) {
    require_once APP_ROOT.'app/model/'.strtolower($class_name) . '.php';
}
class DateController extends InitController {

    public function __construct() {
        parent::__construct();

    }

    public function actionDel() {
        if($_REQUEST['a']) {
            if($_REQUEST['start_time']) {
                $start_time = $_REQUEST['start_time'];
            }else {
                return self::sysMessage("开始时间不能为空！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
            if($_REQUEST['end_time']) {
                $end_time = $_REQUEST['end_time'];
            }else {
                return self::sysMessage("结束时间不能为空！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
            if  ($end_time < $start_time) {
                return self::sysMessage("开始时间不能于结束时间！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
            $type = $_REQUEST['down_type'];
            $reslut = DateModel::delDate($start_time,$end_time,$type);
            if ($reslut){
                return self::sysMessage("删除".$reslut."条记录成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else{
                 return self::sysMessage("删除不成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        }

    }
    public function actionDown() {
        
		if($_REQUEST['do']=="down")
		{
			$down_type = $_REQUEST['down_type'];
			$start_time = $_REQUEST['start_time'];
			$end_time = $_REQUEST['end_time'];

			$query = DateModel::downDate($start_time,$end_time,$down_type);
			$timestamp=time();
			$file_name = './tmp/'.$timestamp . '.txt';
			$resourse = fopen($file_name,'a');
			fwrite($resourse,"pid,email,send_time,open_ip,open_time,open_url,click_ip,click_time,click_url\r\n");
			foreach ($query as $key => $row) {
				
				fwrite($resourse,"{$row['pid']},{$row['email']},{$row['send_time']},{$row['open_ip']},{$row['open_time']},{$row['open_url']},{$row['click_ip']},{$row['click_time']},{$row['click_url']}\r\n");
				
			}
			fclose($resourse);
			//$filename= './tmp/'.$timestamp . '.zip';
			//$z = new PHPZip(); //新建立一个zip的类
			//$files[]=$file_name;
			//$z -> Zip($files, $filename); //添加文件列表
			//下载咋办，你想个主意。
                         $site = self::$conf->confValue("domain", "1");
                         $url = $site.$file_name ;
                         die("<script LANGUAGE='javascript'>window.open('".$url."') ;history.go(-1);</script>");
			
			//
		}
    }
    public function actionList() {
        if($_REQUEST['id']) {
            if ($_REQUEST['a'] == "del") {
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                //print_r($userType);
                if($userInfo["user_type"] == 2) {
                    $del = ItemModel::delItem($_REQUEST['id']);
                }else {
                    die("<script LANGUAGE='javascript'>alert('对不起，您无权删除！');history.go(-1);</script>");
                }
            }
        }
        if($_REQUEST['p']) {
            $page = $_REQUEST['p'];
        }else {
            $page = 1;
        }
        $pageSize = 20;
        $sum = ItemModel::getListCount();
        if (ceil ( $sum / $pageSize ) <= $page && ceil ( $sum / $pageSize ) != 0) {
            $page = ceil ( $sum / $pageSize );
        }
        $liststart=$pageSize * ($page - 1);
        $result = ItemModel::getList($page,$liststart,$pageSize);
        Y::$tpl->set("list",$result);
    }
    public function actionEdit() {
        $id=$_REQUEST['id'];
        if ($_REQUEST['a'] == d) {
            $s = array();

            $s['user_id'] = $_REQUEST['user_id'];
            $s['project_code'] = $_REQUEST['project_code'];
            $s['start_time']=$_REQUEST['start_time'];
            $s['end_time']=$_REQUEST['end_time'];
            $s['state']=md5($_REQUEST['state']);
            $s['title']=$_REQUEST['title'];
            $s['introduction']=$_REQUEST['introduction'];
            $s['created_time']=date("Y-m-d H:i:s");
            if ($saveUser) {
                return self::sysMessage("修改用户成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else {
                return self::sysMessage("修改用户失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        }
        $itemInfo = ItemModel::getOneItem($id);
        Y::$tpl->set("item",$itemInfo);
    }




}

?>
